package com.bcbim.api_web.weather.controller;

import com.alibaba.fastjson.JSONObject;
import com.bcbim.common.log.annotation.RecordLog;
import com.bcbim.common.response.vo.ReturnMsg;
import com.bcbim.common.sys.Constant;
import com.bcbim.common.util.GuidUtil;
import com.bcbim.smart_water_interface.weather.dao.WeatherDAO;
import com.bcbim.smart_water_interface.weather.service.WeatherService;
import com.bcbim.smart_water_interface.weather.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @description: 气象信息 控制类
 * @author: zhangfan
 * @time: 2021/1/18
 */
@Slf4j
@RestController
@RequestMapping("/weather")
public class WeatherController {

    public static final String VERSIONV1 = "v1";            // 七日天气接口
    public static final String VERSIONV6 = "v6";            // 实况天气接口

    @Reference(protocol = "dubbo", group = "weather", version = "1.0.0")
    private WeatherService weatherService;

    /**
     * @Description: 获取当前天气信息
     * @Param: null
     * @Author: zhangfan
     * @date: 2021/1/18
     */
    @RecordLog(content = "获取当前天气信息")
    @RequestMapping(value = "/getPresentWeather", method = RequestMethod.POST)
    public ReturnMsg getPresentWeather() {

        JSONObject jsonObject = (JSONObject) JSONObject.toJSON(weatherService.listWeather(VERSIONV6));
        PresentWeatherInfo presentWeatherInfo = JSONObject.toJavaObject(jsonObject, PresentWeatherInfo.class);

        return ReturnMsg.consSuccess(presentWeatherInfo);
    }

    /**
     * @Description: 获取本日天气信息
     * @Param: null
     * @Author: zhangfan
     * @date: 2021/1/18
     */
    @RecordLog(content = "获取本日天气信息")
    @RequestMapping(value = "/listTodayWeather", method = RequestMethod.POST)
    public ReturnMsg listTodayWeather() {

        JSONObject jsonObject = (JSONObject) JSONObject.toJSON(weatherService.listWeather(VERSIONV1));
        TodayWeather todayWeather = JSONObject.toJavaObject(jsonObject, TodayWeather.class);

        HourWeather[] hourWeather = todayWeather.getData();

        return ReturnMsg.consSuccess(hourWeather[0]);
    }

    /**
     * @Description: 获取未来七天天气信息
     * @Param: null
     * @Author: zhangfan
     * @date: 2021/1/18
     */
    @RecordLog(content = "获取未来七天天气信息")
    @RequestMapping(value = "/listSevenDaysWeather", method = RequestMethod.POST)
    public ReturnMsg listSevenDaysWeather() {

        JSONObject jsonObject = (JSONObject) JSONObject.toJSON(weatherService.listWeather(VERSIONV1));
        SevenDaysWeather sevenDaysWeather = JSONObject.toJavaObject(jsonObject, SevenDaysWeather.class);

        SevenDaysWeatherInfo[] sevenDaysWeatherInfos = sevenDaysWeather.getData();

        return ReturnMsg.consSuccess(sevenDaysWeatherInfos);
    }

    @Scheduled(cron = "0 0 1  * * ?")
    public void insertWeather() throws ParseException {
        JSONObject jsonObject = (JSONObject) JSONObject.toJSON(weatherService.listWeather(VERSIONV6));
        SevenDaysWeatherInfo weatherInfo = JSONObject.toJavaObject(jsonObject, SevenDaysWeatherInfo.class);
        int average = (Integer.parseInt(weatherInfo.getTem1()) + Integer.parseInt(weatherInfo.getTem2())) / 2;
        WeatherDAO weatherDAO = new WeatherDAO();
        weatherDAO.setId(GuidUtil.get())
                .setRecordTime(LocalDate.parse(weatherInfo.getDate(), DateTimeFormatter.ofPattern("yyyy-MM-dd")))
                .setMaxTemperature(Integer.parseInt(weatherInfo.getTem1()))
                .setMinTemperature(Integer.parseInt(weatherInfo.getTem2()))
                .setAverageTemperature(Integer.parseInt(String.valueOf(average)))
                .setDel(Constant.NOT_DEL)
                .setCreateTime(LocalDateTime.now());
        int i = weatherService.insert(weatherDAO);
        if (!(i > 0)) {
            System.out.println("数据添加失败！");
        }
    }

    @RequestMapping("/listWeatherAnalysis")
    public ReturnMsg listWeatherAnalysis() {
        List<WeatherAnalysisVO> list = weatherService.listWeatherAnalysis();
        return ReturnMsg.consSuccess(list);
    }
}
